import React, { useEffect,useState } from 'react'
import { Button, Input, Form } from 'antd'
import { useSelector } from 'react-redux'
import { $resetPwd } from '../../api/adminApi'
import MyNotification from '../../components/MyNotification/MyNotification';
export default function UpdatePwd() {
    let [notiMsg, setNotiMsg] = useState({ type: '', description: '' })
    const adminSlice = useSelector(state => state.adminSlice)
    const { admin } = adminSlice
    let [form] = Form.useForm();
    useEffect(() => {
        form.setFieldValue('id', admin.id)
    }, [admin.id])
    const onFinish = (values) => {
        $resetPwd(values).then(({ success, message }) => {
            if (success) {
                setNotiMsg({ type: 'success', description: message })
            } else {
                setNotiMsg({ type: 'error', description: message })
            }
        })
    }
    const clear = () => {
        form.resetFields()
    }
    return (
        <>
            <Form
                name="basic"
                form={form}
                labelCol={{
                    span: 4,
                }}
                wrapperCol={{
                    span: 18,
                }}
                style={{
                    maxWidth: 600,
                }}
                // initialValues={{
                //     remember: true,
                // }}
                onFinish={onFinish}
                // onFinishFailed={onFinishFailed}
                autoComplete="off"
            >
                <Form.Item
                    label="编号"
                    name="id"
                    hidden
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="原始密码"
                    name="oldLoginPwd"
                    rules={[
                        {
                            required: true,
                            message: '请输入原始密码',
                        },
                    ]}

                >
                    <Input.Password />
                </Form.Item>
                <Form.Item
                    label="最新密码"
                    name="newLoginPwd"
                    rules={[
                        {
                            required: true,
                            message: '请输入最新密码',
                        },
                    ]}

                >
                    <Input.Password />
                </Form.Item>
                <Form.Item
                    label="确认密码"
                    name="newLoginPwd2"
                    dependencies={['newLoginPwd']}
                    rules={[
                        {
                            required: true,
                            message: '请再次确认密码',
                        }, ({ getFieldValue }) => ({
                            validator(_, value) {
                                if (!value || getFieldValue('newLoginPwd') === value) {
                                    return Promise.resolve();
                                }
                                return Promise.reject(new Error('两次密码输入不匹配!'));
                            },
                        }),
                    ]}

                >
                    <Input.Password />
                </Form.Item>
                <Form.Item
                    wrapperCol={{
                        offset: 4,
                        span: 16,
                    }}
                >
                    <Button type="primary" htmlType="submit">
                        修改
                    </Button>
                    <Button onClick={clear} style={{ marginLeft: '10px' }}>
                        取消
                    </Button>
                </Form.Item>
            </Form>
            <MyNotification notiMsg={notiMsg} />
        </>
    )
}
